package com.frxuexi.park2302.my

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.MyApproveBean
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.StatusBean
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2302.R
import com.frxuexi.park2302.databinding.ActivityMyApproveBinding
import com.frxuexi.park2302.my.adapter.MyApproveAdapter
import com.frxuexi.park2302.my.adapter.MyApproveStatusAdapter
import com.frxuexi.visit.adapter.VisitStatusAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/MyApproveActivity")
@AndroidEntryPoint
class MyApproveActivity : BaseActivity<ActivityMyApproveBinding,MyIntent,MyViewModel>() {
    override val binding: ActivityMyApproveBinding by lazy { ActivityMyApproveBinding.inflate(layoutInflater) }
    override val vm: MyViewModel by viewModels<MyViewModel>()
    val myApproveStatusAdapter by lazy { MyApproveStatusAdapter() }
    val myApproveAdapter by lazy { MyApproveAdapter() }
    val parksList= mutableListOf<ParkBeanItem>()
    val visitsList= mutableListOf<VisitBeanItem>()
    val newsList= mutableListOf<NewsBeanItem>()
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val items = it.data as Collection<ParkBeanItem>
                                items.forEach {
                                    it.itemType=0
                                }
                                parksList.addAll(items)
                                myApproveAdapter.data.clear()
                                myApproveAdapter.addData(items)
                                myApproveAdapter.data.addAll(myApproveAdapter.data.filterNot { it is ParkBeanItem })
                                myApproveAdapter.notifyDataSetChanged()
                            }
                            RequestType.MYVISIT->{
                                val items = it.data as Collection<VisitBeanItem>
                                items.forEach {
                                    it.itemType=1
                                }
                                visitsList.addAll(items)
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()
                            }
                            RequestType.NEWS->{
                                val items = it.data as Collection<NewsBeanItem>
                                items.forEach {
                                    it.itemType=2
                                }
                                newsList.addAll(items)
                                myApproveAdapter.addData(items)
                                myApproveAdapter.notifyDataSetChanged()
                            }
                            RequestType.UPDATE->{
                                parksList.clear()
                                vm.sendIntent(MyIntent.getParks("标准车位"))
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        val statusList = listOf<StatusBean>(
            StatusBean("全部", true),
            StatusBean("车位审核", false),
            StatusBean("来访审核", false),
            StatusBean("新闻审核", false)
        )
        myApproveStatusAdapter.addData(statusList)
        binding.appRv.apply {
            adapter=myApproveStatusAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity,RecyclerView.HORIZONTAL,false)
        }

        myApproveStatusAdapter.setOnItemClickListener { adapter, view, position ->
            myApproveStatusAdapter.data.forEach {
                it.isCheck=false
            }
            myApproveStatusAdapter.data.get(position).isCheck=true
            myApproveStatusAdapter.notifyDataSetChanged()

            myApproveAdapter.apply {
                data.clear()
                if(position==0){
                    addData(parksList)
                    addData(visitsList)
                    addData(newsList)
                }else if(position==1){
                    addData(parksList)
                }else if(position==2){
                    addData(visitsList)
                }else{
                    addData(newsList)
                }
                notifyDataSetChanged()
            }
        }

        binding.rvApprove.apply {
            adapter=myApproveAdapter
            layoutManager=LinearLayoutManager(this@MyApproveActivity)
        }
        myApproveAdapter.addChildClickViewIds(R.id.btn_ok,R.id.btn_no)
        myApproveAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = myApproveAdapter.data.get(position)
            when(view.id){
                R.id.btn_ok->{
                    val item = bean as ParkBeanItem
                    vm.sendIntent(MyIntent.updateParkStatus(item.id!!,"维护中"))
                }
                R.id.btn_no->{
                    val item = bean as ParkBeanItem
                    vm.sendIntent(MyIntent.updateParkStatus(item.id!!,"已占用"))
                }
            }
        }


        vm.sendIntent(MyIntent.getParks("标准车位"))
        vm.sendIntent(MyIntent.getVisits)
        vm.sendIntent(MyIntent.getNews)
    }

}